package com.ruoyi.web.controller.tool.voiceCall.utils;

import cn.hutool.extra.spring.SpringUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.aliyun.dyvmsapi20170525.Client;
import com.aliyun.dyvmsapi20170525.models.QueryCallDetailByCallIdRequest;
import com.aliyun.dyvmsapi20170525.models.QueryCallDetailByCallIdResponse;
import com.aliyun.dyvmsapi20170525.models.SingleCallByVoiceRequest;
import com.aliyun.dyvmsapi20170525.models.SingleCallByVoiceResponse;
import com.aliyun.tea.TeaException;
import com.aliyun.teautil.models.RuntimeOptions;

import com.ruoyi.web.controller.tool.voiceCall.enums.VoiceTypeEnum;
import com.ruoyi.web.controller.tool.voiceCall.service.CommonService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;

/**
 * 自动语音电话
 */
@Slf4j
public class AutoCallUtil {

    public static JSONObject call(String phone) {
        JSONObject jsonObject = null;
        try {
            Client client = Sample.createClient("LTAI5tLnPaUP2T1xCxx51QmU", "i4xeeiokkolIVO5ftFf8E1PMlBnrp3");
            SingleCallByVoiceRequest singleCallByVoiceRequest = new SingleCallByVoiceRequest()
                    .setCalledNumber(phone)
                    .setVoiceCode("f36b5252-0ece-42c4-bb5f-05db70dea956.wav")
                    .setPlayTimes(1)
                    .setVolume(8)
                    .setSpeed(1)
                    .setOutId(VoiceTypeEnum.WAMI_VOICE.name());
            RuntimeOptions runtime = new RuntimeOptions();
            // 复制代码运行请自行打印 API 的返回值
            SingleCallByVoiceResponse response = client.singleCallByVoiceWithOptions(singleCallByVoiceRequest, runtime);
            jsonObject = JSONUtil.parseObj(response.getBody());
            //记录限流使用redis数据
            CommonService commonService = SpringUtil.getBean(CommonService.class);
            commonService.setPhoneBeLimit(phone);
            log.info("拨打语音通知回调信息====="+jsonObject);
        } catch (Exception e) {
            TeaException error = new TeaException(e.getMessage(), e);
            log.info("自动语音响应错误{}", error.message);
        }
        return jsonObject;
    }

    public static JSONObject selectCall(String callId,Long times){
        JSONObject jsonObject = null;
        try {
            Client client = Sample.createClient("LTAI5tLnPaUP2T1xCxx51QmU", "i4xeeiokkolIVO5ftFf8E1PMlBnrp3");
            QueryCallDetailByCallIdRequest queryCallDetailByCallIdRequest = new QueryCallDetailByCallIdRequest()
                    .setCallId(callId)
                    .setProdId(11000000300006L)
                    .setQueryDate(times);
            RuntimeOptions runtime = new RuntimeOptions();
            QueryCallDetailByCallIdResponse response =client.queryCallDetailByCallIdWithOptions(queryCallDetailByCallIdRequest, runtime);
            jsonObject = JSONUtil.parseObj(response.getBody().getData());
            log.info("语音通知呼叫详情==========="+jsonObject);
        }catch (Exception e){
            TeaException error = new TeaException(e.getMessage(), e);
            log.info("自动语音响应错误{}", error.message);
        }
        return jsonObject;
    }

    /**
     * 语音电话
     * 返回callId
     */
    public static String voiceCall(String voiceCode,String phone){
        if(!StringUtils.hasText(voiceCode) || !StringUtils.hasText(phone)){
            return null;
        }
        // 复制代码运行请自行打印 API 的返回值
        SingleCallByVoiceResponse response = null;
        try {
            Client client = Sample.createClient("LTAI5tLnPaUP2T1xCxx51QmU", "i4xeeiokkolIVO5ftFf8E1PMlBnrp3");
            SingleCallByVoiceRequest singleCallByVoiceRequest = new SingleCallByVoiceRequest()
                    .setCalledNumber(phone)
                    .setVoiceCode(voiceCode)
                    .setPlayTimes(1)
                    .setVolume(8)
                    .setSpeed(1)
                    .setOutId(VoiceTypeEnum.WAMI_VOICE.name());
            RuntimeOptions runtime = new RuntimeOptions();
            response = client.singleCallByVoiceWithOptions(singleCallByVoiceRequest, runtime);
            //记录限流使用redis数据
            CommonService commonService = SpringUtil.getBean(CommonService.class);
            commonService.setPhoneBeLimit(phone);
        } catch (Exception e) {
            log.info("自动语音响应失败");
            e.printStackTrace();
        }
        if(null != response && null != response.getBody()){
            if(StringUtils.hasText(response.getBody().getCode())){
                log.info("电话_语音码_响应码 {}-{}-{}",phone,voiceCode,response.getBody().getCode());
                if("OK".equals(response.getBody().getCode())){
                    String callId = response.getBody().getCallId();
                    log.info("电话_callId {}-{}",phone,callId);
                    return callId;
                }else{
                    log.info("电话_响应msg:{}-{}",phone,response.getBody().getMessage());
                }
            }
        }
        return null;
    }

    public static void main(String[] args) {
        String s = AutoCallUtil.voiceCall("cd37f905-9ceb-4ace-8475-d490e50ff1c1.wav", "17772450614");
//        long currentTimeMillis = System.currentTimeMillis();
//        log.info("callId:_时间戳:{}-{}",s,currentTimeMillis);
//
//        JSONObject jsonObject = AutoCallUtil.selectCall("136103488875^122867038876", 1689066302919L);
//        log.info("jsonObject{}",jsonObject.toJSONString(2));
    }


//    public static void main(String[] args) throws Exception {
//        JSONObject jsonObject = null;
//        Client client = Sample.createClient("LTAI5tLnPaUP2T1xCxx51QmU", "i4xeeiokkolIVO5ftFf8E1PMlBnrp3");
//        SingleCallByVoiceRequest singleCallByVoiceRequest = new SingleCallByVoiceRequest()
//                .setCalledNumber("18883301296")
//                .setVoiceCode("f36b5252-0ece-42c4-bb5f-05db70dea956.wav")
//                .setPlayTimes(1)
//                .setVolume(8)
//                .setSpeed(1);
//        RuntimeOptions runtime = new RuntimeOptions();
//        // 复制代码运行请自行打印 API 的返回值
//        SingleCallByVoiceResponse response = client.singleCallByVoiceWithOptions(singleCallByVoiceRequest, runtime);
//        jsonObject = JSONUtil.parseObj(response.getBody());
//        log.info("拨打语音通知回调信息====="+jsonObject);
//    }
}
